﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_TransportBillGroupController : BaseApiController
    {
        #region DI

        public PB_TransportBillGroupController(IPB_TransportBillGroupBusiness pB_TransportBillGroupBus)
        {
            _pB_TransportBillGroupBus = pB_TransportBillGroupBus;
        }

        IPB_TransportBillGroupBusiness _pB_TransportBillGroupBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_TransportBillGroup>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_TransportBillGroupBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_TransportBillGroup> GetTheData(IdInputDTO input)
        {
            return await _pB_TransportBillGroupBus.GetTheDataAsync(input.id);
        }

        #endregion

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        #region 提交
        [HttpPost]
        public async Task SaveData(SavaPB_TransportBillGroupDto data)
        {
            var where = LinqHelper.True<PB_TransportBillGroup>();
            if (!data.ParentId.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_TransportBillGroup, bool>(
                        ParsingConfig.Default, false, $@"ParentId.Equals(@0)", data.ParentId);
                where = where.And(newWhere);
            }
            List<PB_TransportBillGroup> PB_TransportBillGroupList = new List<PB_TransportBillGroup>();
            await _pB_TransportBillGroupBus.DeleteOtherIdDataAsync(where);
            //if (data.id.IsNullOrEmpty())
            //{
                //InitEntity(data);
                foreach (var item in data.DeliveryNoticeIds)
                {
                 
                    PB_TransportBillGroup transportBillGroup = new PB_TransportBillGroup();
                    transportBillGroup.Id = IdHelper.GetId();
                    transportBillGroup.ParentId = data.ParentId;
                    transportBillGroup.DeliveryNoticeId = item;
                    transportBillGroup.Deleted = false;
                    transportBillGroup.CreateTime = System.DateTime.Now;
                    PB_TransportBillGroupList.Add(transportBillGroup);
                }
                await _pB_TransportBillGroupBus.AddDataAsync(PB_TransportBillGroupList);
            //}
            //else
            //{
            //    //修改
            //    foreach (var item in data.ParentId)
            //    {
            //        PB_TransportBillGroup transportBillGroup = new PB_TransportBillGroup();
            //        transportBillGroup.Id = data.id;
            //        transportBillGroup.ParentId = data.ParentId;
            //        transportBillGroup.DeliveryNoticeId = item.ToString();
            //        await _pB_TransportBillGroupBus.UpdateDataAsync(transportBillGroup);
            //    }
            //}
        }
       

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_TransportBillGroupBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 初始化PB_TransportBillGroup表数据
        /// 只作用2022-03更新生产库使用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task RechargeBillGroupAsync()
        {
            await _pB_TransportBillGroupBus.RechargeBillGroupAsync();
        }

        #endregion
    }
}